package com.dcy.znoa.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pengyu on 2017/6/13.
 */
public class DateTimeUtil {
    public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat datetimeFormat_1 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat datetimeFormat_HMS = new SimpleDateFormat("HH:mm:ss");

    /**
     * 获取指定时间格式
     *
     * @param format
     * @return
     */
    public static String geSpecifiedFormatTime(String format) {
        try {
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(currentTime);
        } catch (DateTimeException ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * 获取时间
     *
     * @param date
     * @return
     */
    public static Date getSpecifiedFormatDate(String date) {
        try {
//            SimpleDateFormat formatter = new SimpleDateFormat();
            return datetimeFormat.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 获取时间 只要年月日
     *
     * @param date
     * @return
     */
    public static Date getYMRDate(String date) {
        try {
            return dateFormat.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 获取时间 只要时分秒
     *
     * @param date
     * @return
     */
    public static Date getHMSDate(String date) {
        if(date == null){
            return null;
        }
        try {
            return datetimeFormat_HMS.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    public static Date getYMRDate(Date date) {
        if(date == null){
            return null;
        }
        try {
            String asd = dateFormat.format(date);
            return  dateFormat.parse(asd);
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    /**
     * 获取时间 只要时分秒
     *
     * @param date
     * @return
     */
    public static Date getHMSDate(Date date) throws ParseException {
        String asd = datetimeFormat_HMS.format(date);
        return  datetimeFormat_HMS.parse(asd);
    }
    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

    public static Date addDay(Date date,int day) throws ParseException {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + day);
        return dft.parse(dft.format(cal.getTime()));
    }

    public static Date subDay(Date date,int day) throws ParseException {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - day);
        return dft.parse(dft.format(cal.getTime()));
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrDateToDate(String str) {

        if (str != null && !str.equals("")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                format.setLenient(false);
                date = format.parse(str);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        } else {
            return null;
        }

    }

    public static void main(String args[]) throws ParseException {
//        System.out.print(getHMSDate("01:02:03"));

        String s = "1988-04-16 ";
//        Date date = new Date();
//        DateFormat df = new SimpleDateFormat("HH:mm:ss");
//        s = s + df.format(date);
//        System.out.println(s);

        String ruleSTStr = DateTimeUtil.datetimeFormat_HMS.format(new Date());
        System.out.println(ruleSTStr);
        Date ruleStYMRHMS = DateTimeUtil.datetimeFormat.parse(s + ruleSTStr);
        System.out.println(ruleStYMRHMS);
    }

}
